/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.procurement.web;

import java.util.List;
import java.util.Set;

import com.javaeye.procurement.dto.MaterialsReceiveOrder;
import com.javaeye.procurement.dto.MaterialsReceiveOrderDetail;
import com.javaeye.procurement.dto.SupplierOrder;
import com.javaeye.procurement.service.IMaterialsReceiveOrderService;
import com.javaeye.procurement.service.ISupplierOrderService;
import com.javaeye.store.dto.Materials;
import com.javaeye.store.service.IMaterialsService;
import com.javaeye.common.util.NumberUtils;
import com.javaeye.common.web.PageInfo;
import com.javaeye.delivery.dto.OrderCheck;

import com.opensymphony.xwork2.ActionSupport;

public class MaterialsReceiveOrderAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7408989332825417843L;
	
	private SupplierCondition condition;
	
	private PageInfo pageInfo;
	
	private List<MaterialsReceiveOrder> orderList;
	
	private String orderId;
	
	/**
	 * 检验信息
	 */
	private List<OrderCheck> checkResults;
	
	/**
	 * 收货单信息
	 */
	private MaterialsReceiveOrder order;
	
	/**
	 * 收货单明细物料ID
	 */
	private List<Integer> materialsIds;
	
	private List<String> rootCategorys;
	
	private List<String> categorys;
	
	private List<Float> materialsNums;
	
	private List<Double> materialsPrices;
	
	private List<Double> materialsAmounts;
	
	private List<String> planNos;
	
	private List<String> remarks;
	
	private List<String> materialsNos;
	
	private List<String> materialsSkus;
	
	private List<Integer> detailIds;
	
	/**
	 * 实际收货数量
	 */
	private List<Float> realNumbers;
	
	/**
	 * 收货单sevice
	 */
	protected IMaterialsReceiveOrderService materialsReceiveOrderService;
	
	private ISupplierOrderService supplierOrderService;
	
	private IMaterialsService materialsService;
	
	public MaterialsReceiveOrderAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new SupplierCondition();
		condition.setStatus(new Integer[]{-1});
		condition.setContractNo("");
		condition.setSupplierName("");
		condition.setMaterialsName("");
		condition.setOrderId("");
		condition.setSkuId(-1);
	}
	
	public void setMaterialsReceiveOrderService(
			IMaterialsReceiveOrderService materialsReceiveOrderService) {
		this.materialsReceiveOrderService = materialsReceiveOrderService;
	}

	public void setSupplierOrderService(ISupplierOrderService supplierOrderService) {
		this.supplierOrderService = supplierOrderService;
	}

	public void setMaterialsService(IMaterialsService materialsService) {
		this.materialsService = materialsService;
	}
	
	/**
	 * 查询所有收货单列表
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsReceiveOrderList() throws Exception {
		orderList = materialsReceiveOrderService.getMaterialsReceiveOrderList(condition, pageInfo);
		return SUCCESS;
	}
	
	/**
	 * 查询等待收货单列表
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsReceiveOrderWaitList() throws Exception {
		condition.setStatus(new Integer[]{MaterialsReceiveOrder.ORDER_STATES_CREATED});
		orderList = materialsReceiveOrderService.getMaterialsReceiveOrderList(condition, pageInfo);
		return SUCCESS;
	}
	
	/**
	 * 查询收货单信息
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsReceiveOrderInfo() throws Exception {
		order = materialsReceiveOrderService.getMaterialsReceiveOrderInfo(orderId);
		//查询检验结果
		checkResults = materialsReceiveOrderService.getCheckResult(orderId);
		return SUCCESS;
	}
	
	/**
	 * 保存新建收货单
	 * @return
	 * @throws Exception
	 */
	public String saveMaterialsReceiveOrder() throws Exception {
		SupplierOrder sOrder = supplierOrderService.getSupplierOrderInfo(orderId);
		order.setSupplierOrderId(sOrder.getId());
		order.setSupplierId(sOrder.getSupplierId());
		order.setSupplierName(sOrder.getSupplierName());
		order.setContractNo(sOrder.getContractNo());
		order.setCreateDate(sOrder.getCreateDate());
		order.setTotleAmount(NumberUtils.formatDouble(order.getTotleAmount()));
		for (int i = 0; i < materialsIds.size(); i++) {
			String materialsNo= materialsNos.get(i);
			Materials materials = materialsService.getMaterialsByCode(materialsNo);
			
			MaterialsReceiveOrderDetail detail = new MaterialsReceiveOrderDetail();
			detail.setMaterialsId(materialsIds.get(i));
			detail.setMaterialsNo(materialsNo);
			detail.setMaterialsCategory(categorys.get(i));
			detail.setMaterialsRootCategory(rootCategorys.get(i));
			detail.setMaterialsName(materials.getName());
			detail.setMaterialsType(materials.getType());
			detail.setMaterialsSku(materialsSkus.get(i));
			detail.setNumber(materialsNums.get(i));
			detail.setUnit(materials.getUnits());
			detail.setPrice(materialsPrices.get(i));
			detail.setAmount(NumberUtils.formatDouble(materialsAmounts.get(i)));
			detail.setPlanNo(planNos.get(i));
			detail.setRemark(remarks.get(i));
			
			order.addDetail(detail);
		}
		materialsReceiveOrderService.saveMaterialsReceiveOrder(order);
		return SUCCESS;
	}
	
	/**
	 * 保存收货信息
	 * @return
	 * @throws Exception
	 */
	public String saveMaterialsReceiveInfo() throws Exception {
		MaterialsReceiveOrder dbOrder = 
			materialsReceiveOrderService.getMaterialsReceiveOrderInfo(orderId);
		Set<MaterialsReceiveOrderDetail> details = dbOrder.getOrderDetails();
		for (int i = 0; i < detailIds.size(); i++) {
			int detailId = detailIds.get(i);
			MaterialsReceiveOrderDetail detail =
				getMaterialsReceiveOrderDetail(detailId, details);
			if (detail != null) {
				detail.setRealNumber(realNumbers.get(i));
				//计算实际的金额
				double amount = detail.getRealNumber() * detail.getPrice();
				detail.setAmount(NumberUtils.formatDouble(amount));
			}
		}
		//重新计算总金额
		double totleAmount = 0;
		for (MaterialsReceiveOrderDetail detail : details) {
			totleAmount += detail.getAmount();
		}
		dbOrder.setTotleAmount(NumberUtils.formatDouble(totleAmount));
		dbOrder.setRealArriveDate(order.getRealArriveDate());
		materialsReceiveOrderService.saveMaterialsReceiveInfo(dbOrder);
		return SUCCESS;
	}
	
	public String deleteMaterialsReceiveOrder() throws Exception {
		materialsReceiveOrderService.removeReceiveOrder(orderId);
		queryMaterialsReceiveOrderList();
		return SUCCESS;
	}
	
	private MaterialsReceiveOrderDetail getMaterialsReceiveOrderDetail(
			int detailId, Set<MaterialsReceiveOrderDetail> details) {
		for (MaterialsReceiveOrderDetail detail : details) {
			if (detailId == detail.getId()) {
				return detail;
			}
		}
		return null;
	}

	public SupplierCondition getCondition() {
		return condition;
	}

	public void setCondition(SupplierCondition condition) {
		this.condition = condition;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public List<MaterialsReceiveOrder> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<MaterialsReceiveOrder> orderList) {
		this.orderList = orderList;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public MaterialsReceiveOrder getOrder() {
		return order;
	}

	public void setOrder(MaterialsReceiveOrder order) {
		this.order = order;
	}

	public List<Integer> getMaterialsIds() {
		return materialsIds;
	}

	public void setMaterialsIds(List<Integer> materialsIds) {
		this.materialsIds = materialsIds;
	}

	public List<String> getRootCategorys() {
		return rootCategorys;
	}

	public void setRootCategorys(List<String> rootCategorys) {
		this.rootCategorys = rootCategorys;
	}

	public List<String> getCategorys() {
		return categorys;
	}

	public void setCategorys(List<String> categorys) {
		this.categorys = categorys;
	}

	public List<Float> getMaterialsNums() {
		return materialsNums;
	}

	public void setMaterialsNums(List<Float> materialsNums) {
		this.materialsNums = materialsNums;
	}

	public List<Double> getMaterialsPrices() {
		return materialsPrices;
	}

	public void setMaterialsPrices(List<Double> materialsPrices) {
		this.materialsPrices = materialsPrices;
	}

	public List<Double> getMaterialsAmounts() {
		return materialsAmounts;
	}

	public void setMaterialsAmounts(List<Double> materialsAmounts) {
		this.materialsAmounts = materialsAmounts;
	}
	
	public List<OrderCheck> getCheckResults() {
		return checkResults;
	}

	public void setCheckResults(List<OrderCheck> checkResults) {
		this.checkResults = checkResults;
	}

	public List<String> getPlanNos() {
		return planNos;
	}

	public void setPlanNos(List<String> planNos) {
		this.planNos = planNos;
	}

	public List<String> getRemarks() {
		return remarks;
	}

	public void setRemarks(List<String> remarks) {
		this.remarks = remarks;
	}

	public List<String> getMaterialsNos() {
		return materialsNos;
	}

	public void setMaterialsNos(List<String> materialsNos) {
		this.materialsNos = materialsNos;
	}

	public List<String> getMaterialsSkus() {
		return materialsSkus;
	}

	public void setMaterialsSkus(List<String> materialsSkus) {
		this.materialsSkus = materialsSkus;
	}

	public List<Integer> getDetailIds() {
		return detailIds;
	}

	public void setDetailIds(List<Integer> detailIds) {
		this.detailIds = detailIds;
	}

	public List<Float> getRealNumbers() {
		return realNumbers;
	}

	public void setRealNumbers(List<Float> realNumbers) {
		this.realNumbers = realNumbers;
	}
	
	
}
